/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-业务逻辑层-工作台-接口实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.platform.service.impl;

import com.taipingframework.boot.zookeeper.support.ZookeeperLock;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class ZookeeperLockTest implements Runnable {

    private static Integer baseNum = 0;
    private CountDownLatch countDownLatch;

    public ZookeeperLockTest(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
//CHECKSTYLE:OFF
        ZookeeperLock zkLock = new ZookeeperLock();
        try {
            zkLock.gainLock();

            baseNum += 1;
            String result = Thread.currentThread().getName() + " : " + new Date() + " ---> baseNum = " + baseNum;
            Thread.sleep(200);
            System.out.println(result);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            zkLock.releaseLock();
            countDownLatch.countDown();
        }
//CHECKSTYLE:ON
    }

}
